﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Charities.ModelViews;
using CharitiesLib;
using System.IO;

namespace Charities.Forms
{
    public partial class SuppliesInWhereHouse : Form
    {
        public bool is_loaded=false;
        int userid;
        ModelContainer ctx = new ModelContainer();
        Form fo = null;
        public SuppliesInWhereHouse(int uid, Form frm)
        {
            fo = frm;
            userid = uid;
            MdiParent = frm;
            InitializeComponent();
        }

        private void SuppliesInWhereHouse_Load(object sender, EventArgs e)
        {
            // add this form to window list
            ((Forms.FormMDI)fo).addFormToWindowsList(this);
            is_loaded = true;
            List<SuppliesView> supViews = new List<SuppliesView>();
            var sups = ctx.Supplies.ToList();
            foreach (var i in sups)
            {
                SuppliesView supv = new SuppliesView();
                supv.Name = i.Name;
                supv.Id = i.Id;
                supv.Ammount = i.Amount;
                if (i.UpdateDate != null)
                {
                    supv.LastInsertDate = i.UpdateDate.Value;
                    if (ctx.Users.Where(c => c.Id == i.UpdateUId).Count() > 0)
                    {
                        supv.UserName = ctx.Users.Single(c => c.Id == i.UpdateUId).username;
                    }
                }
                else if(i.InsertDate !=null)
                {
                    supv.LastInsertDate = i.InsertDate.Value;
                    if (ctx.Users.Where(c => c.Id == i.InsertUId).Count() > 0)
                    {
                        supv.UserName = ctx.Users.Single(c => c.Id == i.InsertUId).username;
                    }
                }
                supv.UnitOfMesurement = i.UnitOfMeasurement;
                if(ctx.Users.Where(c=>c.Id==i.UpdateUId).Count()>0)
                {
                    supv.UserName = ctx.Users.Single(c => c.Id == i.UpdateUId).username;
                }
                supViews.Add(supv);
            }
            SuppliesViewBindingSource.DataSource = supViews;
            if (ctx.CharityInfos.Count() > 0)
            {
                CharityInfoBindingSource.DataSource = ctx.CharityInfos.First();
            }
            else
            {
                MessageBox.Show("يجب تنصيب الاعدادات الرئيسية حتى تعمل التقارير بالشكل الامثل ", "تنويه", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            this.reportViewer1.RefreshReport();

        }
        public byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms.ToArray();
        }
        private void SuppliesInWhereHouse_FormClosed(object sender, FormClosedEventArgs e)
        {
            is_loaded = false;
            // remove this form from windows list
            ((Forms.FormMDI)fo).removeFormFromWindowsList(this);
        }

        private void SuppliesInWhereHouse_Activated(object sender, EventArgs e)
        {
            // activate this form and add it windows list as activate one
            ((Forms.FormMDI)this.MdiParent).activateWindow(this);
        }
    }
}
